import jwt from 'jsonwebtoken'
import Comments from '@/model/Comments'
import Sign from '@/model/SignRecord'
import User from '@/model/User'
import List from '@/model/List'
import qs from 'qs'
import moment from 'moment'

class AdminController {
  async getUserComms(ctx) {
    let { body } = ctx.request
    if ((body = {})) {
      let comments = await Comments.getAllComm({})
      ctx.body = {
        code: 200,
        data: comments,
      }
    }
  }
  async delComm(ctx) {
    let body = qs.parse(ctx.request.query)
    let ids = body.ids
    let idList = ids.split(',')
    if (body.ids !== null) {
      let res = await Comments.deleteall(idList)
      if (res.deletedCount != 0) {
        ctx.body = {
          code: 200,
          msg: '删除评论成功',
        }
      } else {
        ctx.body = {
          code: 500,
          msg: '删除评论失败',
        }
      }
    } else {
      return
    }
  }
  async getUser(ctx) {
    let { body } = ctx.request.query
    if (body === undefined || body == null) {
      let user = await User.find({})
      ctx.body = {
        code: 200,
        data: user,
      }
    } else {
      ctx.body = {
        code: 500,
      }
    }
  }
  async delUser(ctx) {
    let body = qs.parse(ctx.request.query)
    let ids = body.ids
    let idList = ids.split(',')
    if (body.ids !== null) {
      let res = await User.deleteall(idList)
      if (res.deletedCount != 0) {
        ctx.body = {
          code: 200,
          msg: '删除用户成功',
        }
      } else {
        ctx.body = {
          code: 500,
          msg: '删除用户失败',
        }
      }
    } else {
      return
    }
  }
  async getSerch(ctx) {
    let body = qs.parse(ctx.request.query)
    let type = body.type
    let value = body.value
    if (value === '' && value != []) {
      let comment = await Comments.findCommList({})
      ctx.body = {
        code: 200,
        data: comment,
      }
    }
    const options = {}
    switch (type) {
      case 'hands':
        options.hands = value
        break
      case 'content':
        options.content = value
        break
      case 'created':
        options.created = value //
        break
      case 'name':
        options.name = value //
        break
      case 'status':
        options.status = value //
        break
      case 'ustatus':
        options.ustatus = value //
        break
      case 'isShow':
        options.isShow = value
        break
      case 'isRead':
        options.isRead = value
        break
    }
    // console.log(body)
    // console.log(options)
    //用户名
    if (options.name || options.ustatus) {
      let userobj = await User.find(options)
      const arr = ['password', 'username']
      arr.map((item) => {
        delete userobj[item]
      })
      let id = userobj[0]._id
      let comments = await Comments.findStatus(id)
      ctx.body = {
        code: 200,
        data: comments,
      }
      return
    }
    //文章及点赞
    if (options.content || options.hands) {
      let comments = await Comments.findSerch(options)
      ctx.body = {
        code: 200,
        data: comments,
      }
      return
    }
    //状态
    // if (options.status) {
    //   console.log(options.status)
    //   let user = User.findTstatus(options.status)
    //   ctx.body = {
    //     code: 200,
    //     data: user,
    //   }
    // }
    if (options.isShow) {
      let comments = await Comments.findShow(options.isShow)
      ctx.body = {
        code: 200,
        data: comments,
      }
      return
    }
    //日期
    if (options.created && options.created[0] !== '') {
      let comments = await Comments.finddate(options)
      ctx.body = {
        code: 200,
        data: comments,
      }
    } else {
      let comment = await Comments.findCommList({})
      ctx.body = {
        code: 200,
        data: comment,
      }
    }
  }

  async save(ctx) {
    let { body } = ctx.request
    console.log(body)
  }
  async getStatData(ctx) {
    let result = {}
    const inforCardData = []
    const time = moment().format('YYYY-MM-DD 00:00:00')
    const userNewCount = await User.find({
      created: { $gte: time },
    }).countDocuments()
    const commentNewCount = await User.find({}).countDocuments()
    const listCount = await List.find({}).countDocuments()

    const starttime = moment(new Date().setHours(0, 0, 0, 0)).isoWeekday(1)
    const endtime = moment(new Date().setHours(0, 0, 0, 0)).isoWeekday(8)
    // console.log(starttime)
    const weekEndCount = await Comments.find({
      created: { $gte: starttime, $lte: endtime },
      isBest: '1',
    }).countDocuments()
    const signCount = await Sign.find({
      created: { $gte: starttime, $lte: endtime },
    }).countDocuments()
    const postCount = await List.find({
      created: { $gte: starttime, $lte: endtime },
    }).countDocuments()
    inforCardData.push(userNewCount)
    inforCardData.push(commentNewCount)
    inforCardData.push(listCount)
    inforCardData.push(weekEndCount)
    inforCardData.push(signCount)
    inforCardData.push(postCount)
    //饼状
    const group = await List.aggregate([
      { $group: { _id: '$catalog', count: { $sum: 1 } } },
    ])
    const pieData = {}
    group.forEach((item) => {
      pieData[item._id] = item.count
    })
    // console.log(group)
    //树状图
    const startMonth = moment(new Date().setHours(0, 0, 0, 0))
      .subtract(5, 'M')
      .date(1).format
    const endMonth = moment(new Date().setHours(0, 0, 0, 0))
      .add(1, 'M')
      .date(1).format
    let monthData = await List.aggregate([
      {
        $match: {
          created: { $gte: new Date(startMonth), $lte: new Date(endMonth) },
        },
      },
      {
        $project: {
          month: { $dateToString: { format: '%Y-%m', date: '$created' } },
        },
      },
      { $group: { _id: '$month', count: { $sum: 1 } } },
      { $sort: { _id: 1 } },
    ])
    console.log(monthData)
    result = {
      inforCardData,
      pieData,
      monthData,
    }
    ctx.body = {
      code: 200,
      data: result,
    }
  }
}

export default new AdminController()
